﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using JPW.OnlinePaperSubmissionSystem.Project.Dto;
using Kendo.DynamicLinq;
using System.Data.Entity;
using Abp.Collections.Extensions;
using Abp.Extensions;

namespace JPW.OnlinePaperSubmissionSystem.Project
{
    
   // [AbpAuthorize(AppPermissions.Pages_Tenant_ParameterType)]
    public class ParameterTypeAppService : AbpZeroTemplateAppServiceBase, IParameterTypeAppService
    {
        private readonly IRepository<ParameterType,long> _parameterTypeRepository;
        public ParameterTypeAppService(IRepository<ParameterType,long> parameterTypeRepository)
        {
            _parameterTypeRepository = parameterTypeRepository;
            
        }
        
        public DataSourceResult KendoGetParameterTypes(DataSourceRequest input)
        {
         var parameterTypes = _parameterTypeRepository
            .GetAll()
            .OrderBy(p => p.Id)
            .MapTo<List<ParameterTypeListDto>>().AsQueryable()
            .ToDataSourceResult(input);

           return parameterTypes;
        }
         public ListResultOutput<ParameterTypeListDto> GetParameterTypes(GetParameterTypeInput input)
        {
            var parameterTypes = _parameterTypeRepository
            .GetAll()
            .WhereIf(
                !input.Filter.IsNullOrEmpty(),
                p => p.Name.Contains(input.Filter) ||
                        p.Code.Contains(input.Filter)
                       
            )
            .OrderBy(p => p.Id)
            .ToList();

            return new ListResultOutput<ParameterTypeListDto>(parameterTypes.MapTo<List<ParameterTypeListDto>>());
        }
         public async Task<ListResultOutput<ParameterTypeListDto>> GetParameterTypesAsync()
        {
            var query = await _parameterTypeRepository.GetAll().OrderBy(p => p.Id).ToListAsync();

            return new ListResultOutput<ParameterTypeListDto>(query.MapTo<List<ParameterTypeListDto>>());

        }
        
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_ParameterType_Delete)]
        public async Task DeleteParameterType(IdInput input)
        {
            await _parameterTypeRepository.DeleteAsync(input.Id);
        }
        
        public async Task CreateOrUpdateParameterType(CreateOrUpdateParameterTypeInput input)
        {
            if (input.ParameterType.ID.HasValue)
            {
                await UpdateParameterTypeAsync(input);
            }
            else
            {
                await CreateParameterTypeAsync(input);
            }
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_ParameterType_Create)]
        public async Task CreateParameterTypeAsync(CreateOrUpdateParameterTypeInput input)
        {
            var parameterType = input.ParameterType.MapTo<ParameterType>();
            await _parameterTypeRepository.InsertAsync(parameterType);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_ParameterType_Update)]
        public async Task UpdateParameterTypeAsync(CreateOrUpdateParameterTypeInput input)
        {
            var parameterType = input.ParameterType.MapTo<ParameterType>();
            await _parameterTypeRepository.UpdateAsync(parameterType);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_ParameterType_Update)]
        public async Task<GetParameterTypeForEditOutput> GetParameterTypeForEdit(NullableIdInput input)
        {
            ParameterType parameterType = null;
            if (input.Id.HasValue) //Editing existing role?
            {
                parameterType = await _parameterTypeRepository.FirstOrDefaultAsync(input.Id.Value);
            }
            return new GetParameterTypeForEditOutput
            {
                ParameterType = parameterType.MapTo<ParameterTypeEditDto>(),
            };
        }
    }
}

